//
//  FoodCtrl.m
//  LvXingPai
//
//  Created by _Junxee on 16/8/13.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "FoodCtrl.h"
#import "FoodModel.h"
#import "FoodCell.h"
#import "AllDesViewController.h"
@interface FoodCtrl ()
@property (weak, nonatomic) IBOutlet UILabel *TopDesLabel;
@property(nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation FoodCtrl
- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@美食",_foodModel.zhName];
    [self loadData];
    [self setBarButStyle];
}

-(void)setFoodModel:(DestinModel *)foodModel{
    _foodModel = foodModel;
}

//阅读全文按钮
- (IBAction)ShowAllDes:(UIButton *)sender {
    AllDesViewController *all = [[AllDesViewController alloc]init];
    all.foModel = [[FoodModel alloc]init];
    [self.navigationController pushViewController:all animated:YES];
}

-(void)setBarButStyle{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_icon_navigation_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"master_search"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.rightBarButtonItem = rightBtn;
}

-(void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)loadData{
    
    AFHTTPRequestOperationManager *Manger = [AFHTTPRequestOperationManager manager];
    Manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [Manger GET:[NSString stringWithFormat:@"http://api.lvxingpai.com/app/poi/restaurants?imgWidth=300&locality=%@&page=0&pageSize=15",_foodModel.pushId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dict[@"result"];
        for (NSDictionary *dict2 in arr) {
            
            FoodModel *fmodel = [[FoodModel alloc]init];
            fmodel.zhName = dict2[@"zhName"];
            fmodel.type = dict2[@"type"];
            NSDictionary *dict4 = dict2[@"locality"];
            fmodel.locatId = dict4[@"id"];
            
            NSArray *arr2 = dict2[@"images"];
            if (arr2.count != 0) {
                NSDictionary *dict3 = arr2[0];
                fmodel.url = dict3[@"url"];
            }
            [self.dataArr addObject:fmodel];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];
    
    [Manger GET:[NSString stringWithFormat:@"http://api.lvxingpai.com/app/guides/locality/%@/restaurant",_foodModel.pushId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"s");
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dict2 = dict[@"result"];
        self.TopDesLabel.text = dict2[@"desc"];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FoodModel *model = self.dataArr[indexPath.row];
    [cell.leftImageV sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"icon_makeOrder_packageNormal"]];
    cell.topLabel.text = model.zhName;
    cell.downLabel.text = model.type;
    
    return cell;
}



@end
